package com.example.glidelearningproject.fragment.start;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CenterInside;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.example.glidelearningproject.GlideApp;
import com.example.glidelearningproject.R;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class PictureDialogFragment extends DialogFragment {
    public static final String PARAM1 = "param1";
    public static PictureDialogFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt(PARAM1,type);
        PictureDialogFragment fragment = new PictureDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_picture,null);
        ImageView imageView = view.findViewById(R.id.imageView);

        int type = getArguments().getInt(PARAM1);
        if (type == 1) {
            RequestOptions shareOptions = new RequestOptions().placeholder(R.mipmap.ic_placeholder)
                    .fitCenter();
            GlideApp.with(this).load("https://upload-images.jianshu.io/upload_images/11345863-772beffcbd1c3557.png?imageMogr2/auto-orient/strip|imageView2/2/w/440/format/webp")
                    .fitCenter()
                    .miniThumb(500)
                    .apply(shareOptions)
                    .into(imageView);
            /*GlideApp.with(this).load("https://upload-images.jianshu.io/upload_images/11345863-772beffcbd1c3557.png?imageMogr2/auto-orient/strip|imageView2/2/w/440/format/webp")
                    .apply(shareOptions)
                    .into(imageView);*/
        }else if(type == 2){
            Glide.with(this)
                    .load("https://upload-images.jianshu.io/upload_images/11345863-772beffcbd1c3557.png?imageMogr2/auto-orient/strip|imageView2/2/w/440/format/webp")
                    .into(new CustomTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            imageView.setImageDrawable(resource);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                            imageView.setImageResource(R.mipmap.ic_placeholder);
                        }
                    });
        }else if(type == 3){
            Observable.just("https://upload-images.jianshu.io/upload_images/2764996-7eccc0b39436932e?imageMogr2/auto-orient/strip|imageView2/2/w/561/format/webp")
                    .map(new Function<String, Bitmap>() {
                        @Override
                        public Bitmap apply(String s) throws Throwable {
                            FutureTarget<Bitmap> futureTarget = Glide.with(PictureDialogFragment.this)
                                    .asBitmap()
                                    .load(s)
                                    .submit(500,500);
                            Bitmap bitmap = futureTarget.get();
                            return bitmap;
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Bitmap>() {
                        @Override
                        public void accept(Bitmap bitmap) throws Throwable {
                            imageView.setImageBitmap(bitmap);
                        }
                    });
        }else if(type == 4){
            GlideApp.with(this).asMyGif()
                    .load("https://www.principlerepo.com/wp-content/uploads/2018/04/wsi-imageoptim-loading-animation.gif")
                    .into(imageView);
        }else if(type == 5){
            GlideApp.with(this)
                    .load("https://upload-images.jianshu.io/upload_images/1993219-21c6174a18833eb5.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp")
                    .transform(new MultiTransformation<>(new FitCenter(),new CenterCrop(),new CenterInside(),new CircleCrop(),new Rotate(90)))
                    .into(imageView);
        }

        builder.setView(view);
        builder.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setLayout(400,400);
        return alertDialog;
    }
}
